package com.spring.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName MyBatisUtil
 * @Description TODO
 * @Author ZhaoKai
 * @Date 2022-02-1113:53
 * @Version 1.0
 **/
public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory = null;


    static {
        String config = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSession getSqlSession() {
        if (sqlSessionFactory != null) {
            return sqlSessionFactory.openSession();
        }
        return null;
    }
}
